$(function () {
    obj.init();

});

var obj = new function () {
    'use strict';
    return {
        form :null,
        dataGrid:null,
        schoolIds:'ALL',
        init:function () {
            this.dataGrid = new xhDataGrid($('#list'), 'appserver/getSchoolFirewall');
            this.dataGrid.parseToolbar().init().activeButton(['bt_list_del'], []);
            this.funObj().bindDataGridEvent();
            this.funObj().initSchoolFilter();
            this.listener();
            this.form = new xhForm($('#xh_form'));
            this.form.parse().bindRequired($('#bt_form_add, #bt_form_edit'));
            xhTool.bindDocKeyDown($('#list_search'));
        },
        listener:function () {
            var $this = this;

            //新增事件按钮
            $('#bt_list_add').on('click',function () {
                xhExtra.forwardToForm();
                $this.form.reset();
                $this.schoolIds = 'ALL';
                $this.funObj().bindSchoolChoice('add');
                // $this.funObj().openAddForm();
            })

            $('#bt_list_del').on('click',function () {
                $this.funObj().askDelQuestion();
            });

            $('#bt_list_search').click(function () {
                $this.funObj().search();
            });

            //重置
            $('#bt_form_reset').click(function(){
                $this.form.reset();
            });
            //返回
            $('#bt_form_back').click(function(){
                xhExtra.backToList();
            });

            $('#bt_form_add').click(function () {
                $this.funObj().saveData();
            });
            
            $('#bt_form_edit').click(function () {
                $this.funObj().modifyData();
            })

        },
        funObj:function () {
            var $this = this;
            return {
                bindDataGridEvent:function () {
                    $this.dataGrid.onDblClickRow = function (rowIndex, rowData) {
                        $this.form.fill(rowData);
                        if(rowData['show'] == '是'){
                            $('[name=show]').val(1);
                        }else {
                            $('[name=show]').val(0);
                        }
                        xhExtra.forwardToForm(true);
                        $this.funObj().bindSchoolChoice('edit');
                        $this.schoolIds = rowData['schoolId'];
                        // window.top.xhTop.showModal({
                        //     title: '编辑学校防火墙',
                        //     content: '<iframe  src="'+baseUrl+'appserver/schoolFirewallForm?schoolId='+rowData['schoolId']+'&packageName='+rowData['packageName']+'"  style="width: 100%;;border-style: none;padding: 0px;height:200px"  scrolling="yes"   ></iframe>',
                        //     button:'提交',
                        //     backClose: false
                        // });
                    }
                },
                bindSchoolChoice:function (tag) {
                    if(tag === 'add') {
                        $('[name=packageName]').removeAttr('readonly');
                        $('#bt_form_school').unbind('click').removeAttr('disabled');
                        $('#bt_form_school').attr('enabled','true').unbind('click').click(function () {
                            window.top.xhTop.showModal({
                                title: '选择学校',
                                content: xhTool.getFrameString('School/schoolChoose', {callback: 'obj.funObj().addAppToSchool', single: 'false'}, 500),
                                size: 'lg',
                                button: '确认'
                            });
                        })
                    }else if(tag === 'edit'){
                        $('[name=packageName]').attr('readonly', 'readonly');
                        $('#bt_form_school').unbind('click').attr('disabled','disable');
                    }

                },
                addAppToSchool:function (schoolIds) {
                    $this.schoolIds = schoolIds.join(',');
                },
                askDelQuestion:function () {
                    var ids    = $this.dataGrid.getSelectValues('id');
                    xhExtra.askChangeStatus('appserver/delSchoolFirewall',{ids:ids},$this.dataGrid,null,function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                    });
                },
                initSchoolFilter:function () {
                    $('#select_school').combobox({ //开启可编辑搜素模式
                        editable:true,
                        filter: function(q, row){
                            var opts = $(this).combobox('options');
                            return row[opts.textField].indexOf(q) >= 0;
                        }
                    });
                },
                search:function () {
                    var key = xhExtra.getValue($('#list_search'));
                    var schoolId = xhExtra.getValue($('#select_school'));
                    $this.dataGrid.reload({key : key , schoolId : schoolId  });
                },
                saveData:function () {
                    $this.form.submit('appserver/addSchoolFirewall',{schoolId:$this.schoolIds},function (res) {
                        $this.dataGrid.reload();
                        window.top.xhTop.showSuccess(res.msg);
                        xhExtra.backToList();
                    })
                },
                modifyData:function () {
                    $this.form.submit('appserver/modifySchoolFireWall',{schoolId:$this.schoolIds},function (res) {
                        $this.dataGrid.reload();
                        window.top.xhTop.showSuccess(res.msg);
                        xhExtra.backToList();
                    })
                }
            }
        }
    }
};